package com.xyy.saas.payment.rpc;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xyy.ec.base.framework.rpc.ApiRPCResult;
import com.xyy.ec.merchant.bussiness.api.account.LoginAccountApi;
import com.xyy.ec.merchant.bussiness.dto.account.LoginAccountDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class EcMerchantRemote {
    @Reference(version = "1.0.0", timeout = 5000)
    private LoginAccountApi loginAccountApi;

    /**
     * 通过手机号查询accountId
     */
    public LoginAccountDto queryMerchantByPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return new LoginAccountDto();
        }
        try {
            ApiRPCResult<LoginAccountDto> accountInfoByMobile = loginAccountApi.getAccountInfoByMobile(phone);
            log.info("queryMerchantByPhone result:{}", accountInfoByMobile.isSuccess());
            if (accountInfoByMobile.isSuccess()) {
                return accountInfoByMobile.getData();
            }
        } catch (Exception e) {
            log.error("queryMerchantByPhone error param:{}", phone, e);
        }
        return new LoginAccountDto();
    }

    /**
     * 通过accountId查询客户信息
     */
    public Map<String, String> queryPhoneByAccountId(List<Long> accountIds) {
        if (CollectionUtils.isEmpty(accountIds)) {
            return new HashMap<>();
        }
        try {
            Map<String, String> res = new HashMap<>();
            ApiRPCResult<List<LoginAccountDto>> rpcResult = loginAccountApi.queryPhoneByAccountIds(accountIds);
            if (rpcResult.isSuccess() && CollectionUtils.isNotEmpty(rpcResult.getData())) {
                for (LoginAccountDto loginAccountDto : rpcResult.getData()) {
                    res.put(String.valueOf(loginAccountDto.getId()), loginAccountDto.getMobile());
                }
                return res;
            }
        } catch (Exception e) {
            log.error("queryCustomByAccountId error param:{}", accountIds, e);
        }
        return new HashMap<>();
    }

}
